<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库编号" prop="outboundId">
        <el-input
          v-model="queryParams.outboundId"
          placeholder="请输入出库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单创建时间" prop="orderCreateTime">
        <el-date-picker clearable
          v-model="queryParams.orderCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择出库状态" clearable>
          <el-option
            v-for="dict in dict.type.outbound_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:list:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="getPackingList"
          v-hasPermi="['system:list:edit']"
        >查看拣货单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:list:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:list:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:list:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="出库编号" align="center" prop="outboundId" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="订单创建时间" align="center" prop="orderCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" prop="outboundStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outbound_status" :value="scope.row.outboundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getPackingList(scope.row)"
            v-hasPermi="['system:list:edit']"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拣货单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库编号" prop="outboundId">
          <el-input v-model="form.outboundId" placeholder="请输入出库编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单编号" :disabled="true"/>
        </el-form-item>
<!--        <el-form-item label="订单创建时间" prop="orderCreateTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.orderCreateTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择订单创建时间"-->
<!--            :disabled="true"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="订单类型" prop="orderType">-->
<!--          <el-select v-model="form.orderType" placeholder="请选择订单类型">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.order_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--:value="parseInt(dict.value)"-->
<!--              :disabled="true"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="出库状态" prop="outboundStatus">
          <el-select v-model="form.outboundStatus" placeholder="请选择出库状态">
            <el-option
              v-for="dict in dict.type.outbound_status"
              :key="dict.value"
              :label="dict.label"
              :disabled="true"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

      <!--   表格存放 packingList     -->
        <!-- 新增表格 -->
        <el-form-item label="拣货明细" prop="packingList">
          <el-table
            :data="packingList"
            border
            stripe
            style="width: 100%"
            max-height="250"
          >
            <el-table-column
              prop="stockTrackingnumber"
              label="快递单号"
              align="center"

            ></el-table-column>
            <el-table-column
              prop="positionType"
              label="仓位类型"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="positionNumber"
              label="仓位编号"
              align="center"
            ></el-table-column>
          </el-table>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList, getList, delList, addList, updateList } from "@/api/system/list";
import {listOutbound} from "@/api/system/outbound";
import {listStore} from "@/api/system/store";

export default {
  name: "List",
  dicts: ['order_type', 'outbound_status'],
  data() {
    return {
      //入库列表
      inList: [],
      //拣货单列表
      packingList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拣货单管理表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outboundId: null,
        orderNumber: null,
        orderCreateTime: null,
        orderType: null,
        outboundStatus: null
      },

      // 查询参数
      // queryOutBoundParams: {
      //   pageNum: 1,
      //   pageSize: 10,
      //   outboundId: null,
      //   warehousing: null,
      //   orderNumber: null,
      //   outboundStatus: null,
      //   userName: null,
      //   loginAccount: null,
      //   outboundTime: null
      // },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outboundId: [
          { required: true, message: "出库编号不能为空", trigger: "blur" }
        ],
        orderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderCreateTime: [
          { required: true, message: "订单创建时间不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        outboundStatus: [
          { required: true, message: "出库状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拣货单管理列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.listList = response.rows;


        this.total = response.total;
        this.loading = false;
      });

      listStore().then(res => {
        this.inList = res.rows;
      });
      // listOutbound().then(response => {
      //   this.outList = response.rows;
      //   this.outList.forEach((item, index) => {
      //     this.listList.push({
      //       outboundId :item.outboundId,
      //       orderNumber: item.orderNumber,
      //       outboundStatus: item.outboundStatus,
      //       // 以下字段不在 outList 中，根据需求补充默认值或留空
      //       orderCreateTime: null,   // 假设需要后续补充或留空
      //       orderType: null         // 假设需要后续补充或留空
      //     });
      //   });
      //
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outboundId: null,
        orderNumber: null,
        orderCreateTime: null,
        orderType: null,
        outboundStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拣货单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getList(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拣货单管理";
      });
    },

    /** 获取拣货单信息*/
    getPackingList(row){//快递单号 仓位类型 仓位编号
      this.reset();
      const id = row.id || this.ids
      getList(id).then(response => {
        this.form = response.data;
        this.packingList = [];
        this.inList.forEach((item, index) => {
          console.log(`第${index}个元素:`, item);
          if(this.form.orderNumber === item.orderNumber){
            this.packingList.push({
              stockTrackingnumber: item.stockTrackingnumber,
              positionType: item.positionType,
              positionNumber: item.positionNumber
            });
          }

        });
        this.open = true;
        this.title = "查看拣货单";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拣货单管理编号为"' + ids + '"的数据项？').then(function() {
        return delList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
